/*
 * Read.cpp
 *
 *  Created on: 17.04.2012
 *      Author: Eugene Kornykhin
 */

#include "Read.h"
#include "VarReference.h"
#include <stdexcept>
#include <iostream>
#include <typeinfo>

Read::Read() {

}

Read::~Read() {

}

void Read::execute(Interpreter& interpreter) {
	const PElement &v = interpreter.popPolizElement();
	try {
		const VarReference &var = dynamic_cast<const VarReference &>(v);
		int value;
		std::cin >> value;
		interpreter.setVarValue(var.getVar(), value);
	} catch(std::bad_cast&) {
		throw std::logic_error("Read statement excepts a variable reference");
	}
}

PolizPosition Read::next_position_after(PolizPosition p) const {
	return p.next();
}

bool Read::isHalt() const {
	return false;
}

Read* Read::clone(const Poliz &) const {
	return new Read();
}

string Read::toString() const {
	return "[READ]";
}
